## author:郑师宇
import requests
import json
from PIL import Image
from io import BytesIO

# 地理编码、逆地理编码
# 地理编码

def gecode(address="str",key="key"):
    """获取高德api的地理编码"""
    gao_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":key,
        "address":address
    }  
    response = requests.get(params=params,url=gao_url)
    result = response.json()
    return result


# 逆地理编码

def regeo(key,location,extensions='all'):
    regeo_url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
      "key":key,
      "location":location,
      "extensions":extensions
}
    response = requests.get(regeo_url,params)
    result = response.json()
    return result



# 路径规划
# 走路规划

def walking(key,origin,destination,sig=None,output="json",callback=None):
    """可以规划100KM以内的步行通勤方案，并且返回通勤方案的数据"""
    url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key":key,
        "origin": origin,
        "destination": destination,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(url, params=params)
    return r.json()

# 公交车路线规划

def bus(key,destination,origin,city,strategy="none",date="none",time="none",cityd="none",extensions="all"):
    """规划综合各类公共（火车、公交、地铁）交通方式的通勤方案，并且返回通勤方案的数据"""
    bus_url='https://restapi.amap.com/v3/direction/transit/integrated?parameters'
    params ={
        "key":key,
        "destination":destination,
        "origin":origin,
        "city":city,
        "cityd":cityd,
        "extensions":extensions,
        "strategy":strategy,
        "date":date,
        "time":time,
        'output':'json'
    }
    response = requests.get(url=bus_url,params=params)
    result = response.json()
    return result


#行政区查询

def district(key,subdistrict,keywords,extentions):
    """根据用户输入的搜索条件可以帮助用户快速的查找特定的行政区域信息"""
    district_url ="https://restapi.amap.com/v3/config/district?parameters"
    params={
        "key":key,
        "keywords":keywords,
        "subdistrict":subdistrict,
        "extensions":extentions
    }
    response =requests.get(url=district_url,params=params)
    result =response.json()[ 'districts'][0]['districts']
    for i in range(len(result)):
        r = result[i]["name"]
        print(r)
    return r


#搜索poi

def text(key,keywords,city,citylimit):
     """通过用POI的关键字进行条件搜索"""
     text_url ="https://restapi.amap.com/v3/place/text?parameters"
     params={
         "key":key,
         "keywords":keywords,
         "city":city,
         "citylimit":citylimit,
    }
     response =requests.get(params=params,url=text_url)
     result=response.json()["pois"]
     return result


#ip定位

def ip(key,ip):
     """根据用户输入的IP地址，能够快速的帮用户定位IP的所在位置"""
     ip_url = "https://restapi.amap.com/v3/ip?parameters"
     params={
       "key":key,
       "ip":ip
       }
     response=requests.get(params=params,url=ip_url)
     result=response.json()
     return result


#静态地图

def staticmap(key,location,zoom,size=None,scale=1,markers=None,labels=None,paths=None,traffic=0,page=None,sig=None)->dict:
    """获取动态地图"""
    url = 'https://restapi.amap.com/v3/staticmap?parameters'
    params={
        'key':key,
        'location':location,
        'zoom':zoom,
        'size':size,
        'scale':scale,
        'markers':markers,
        'labels':labels,
        'paths':paths,
        'traffic':traffic,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = Image.open(BytesIO(response.content))
    return data


#天气查询

def weatherInfo(key,city,extensions=None):
    """根据用户输入的adcode，查询目标区域当前/未来的天气情况"""
    weatherInfo_url="https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params={
        "key":key,
        "city":city,
        "extensions":extensions
    }
    response=requests.get(url= weatherInfo_url,params=params)
    result=response.json()
    return result


#输入提示

def input_tips(key,keyword,poi_type=None,location=None,city=None,citylimit="false",datatype="all",sig=None,output="json",callback=None):
    """提供根据用户输入的关键词查询返回建议列表"""
    input_tips_url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key": key,
        "keywords": keyword,
        "type": poi_type,
        "location": location,
        "city": city,
        "citylimit": citylimit,
        "datatype": datatype,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(input_tips_url, params)
    return r.json()
